#!/bin/sh

if [ $# -ne 2 ]; then
	echo "usage: $0 <copy1> <copy2>"
	exit 2
fi

size1=$(stat -c %s $1)
size2=$(stat -c %s $2)

if [ $size1 -ne $size2 ]; then
	echo "copies size differs"
	exit 2
fi

blocksize=16
numblocks=$((size1/blocksize))
if [ $((size1%blocksize)) -gt 0 ]; then numblocks=$((numblocks + 1)); fi

dumpout=$(mktemp -u)

mkfifo $dumpout
exec 3<>$dumpout
rm -f $dumpout

cat <&3 >/dev/null &

#trap "trap - TERM && kill -- -$$" INT TERM EXIT
trap "pkill -P $$; exit" INT TERM EXIT
trap "head -n1 <&3 >&2 &" USR2

count=0

while [ $count -lt $numblocks ]; do
	skip=$(($count * $blocksize))
	string1=$(hexdump -Cv -n $blocksize -s $skip $1 | head -n1)
	string2=$(hexdump -Cv -n $blocksize -s $skip $2 | head -n1)
	echo $string1 | cut -c1-8 >&3
	
	if [ "$string1" != "$string2" ]; then
		echo "--------------------------------------------------"
		printf "%s\n" "$string1"
		printf "%s\n" "$string2"
	fi
	count=$(($count + 1))
done

exec 3<&-
